<%--
  Created by IntelliJ IDEA.
  User: 刘洪兆0720
  Date: 2022/4/28
  Time: 9:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>上机考试系统</title>
</head>
<body>
<jsp:include page="teacher_head.jsp"></jsp:include>

<div class="container">
    <br/>
    <div class="row">
        <div class="col-md-offset-1 col-md-10">
            <div class="panel panel-info">
                <div class="panel-heading">
                    <h3 class="panel-title">
                        <span class="glyphicon glyphicon-th-list"></span>&nbsp;<font color='red'>${examStartName}</font>
                        考试进行状况
                    </h3>
                </div>
                <div class="panel-body">
                    <table class="table table-bordered">
                        <tr>
                            <th>参加人数</th>
                            <th>已登陆人数</th>
                            <th>未登录人数</th>
                            <th>已提交人数</th>
                            <th>未提交人数</th>
                            <th>操作</th>
                        </tr>
                        <tr>
                            <th id="stu_number"></th>
                            <th id="stu_login"></th>
                            <th id="stu_nologin"></th>
                            <th id="stu_submit"></th>
                            <th id="stu_nosubmit"></th>
                            <th>
                                <button type="button" class="btn btn-outline-info" id="searchStatus">查看详情</button>
                            </th>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- 显示表格数据 -->
    <div class="row" style="margin-top: 30px" id="tableData">
        <div class="col-md-12">
            <strong style="display:block;margin-left: 20px; font-size: 18px">详细信息</strong>
            <!--分页文字信息  -->
            <div class="col-md-8 text-start" id="page_info_area"
                 style="margin-bottom: 10px;margin-left:5px;margin-top:20px;font-size: 15px;"></div>

            <table class="table table-responsive table-bordered table-striped table-hover" id="teas_table">
                <thead class="table-light">
                <tr>
                    <th class="col-md-2">序号</th>
                    <th class="col-md-2">学号</th>
                    <th class="col-md-2">姓名</th>
                    <th class="col-md-2">班级</th>
                    <th class="col-md-2">ip地址</th>
                    <th class="col-md-2">提交状态</th>
                </tr>
                </thead>
                <tbody id="tbody">

                </tbody>
            </table>
        </div>
    </div>

    <!-- 显示分页信息 -->
    <div class="row" style="margin-top: 100px;align-content: center" id="pageId">
        <!-- 分页条信息 -->
        <div class="col-md-12" id="page_nav_area" style="align-self: center"></div>
    </div>
</div>
</body>
<script type="text/javascript">
    var currentPage, totalRecord;

    $(function () {
        var tableData = document.getElementById('tableData');//选取id为test的div元素
        tableData.style.display = 'none';// 隐藏选择的元素
        var pageId = document.getElementById('pageId');//选取id为test的div元素
        pageId.style.display = 'none';// 隐藏选择的元素

        $.post("${path}/getStartExamInfos", function (pageInfo) {
            if (pageInfo.code = 100) {
                var data = pageInfo.extend.lists;
                var stuNumber = 0;
                var stuLogin = 0;
                var stuSubmit = 0;
                if (data != null && data != "") {
                    stuNumber = data.length;
                    for (var i = 0; i < stuNumber; i++) {
                        if (data[i].stu_ip != null) {
                            stuLogin++;
                        }
                        if (data[i].stu_submit != null && data[i].stu_submit != 0) {
                            stuSubmit++;
                        }
                    }
                }
                $("#stu_number").html(stuNumber);
                $("#stu_login").html(stuLogin);
                $("#stu_nologin").html(stuNumber - stuLogin);
                $("#stu_submit").html(stuSubmit);
                $("#stu_nosubmit").html(stuNumber - stuSubmit);
            }
        })
    });

    $('#searchStatus').on('click', function () {
        to_page(1);
    });

    function to_page(pageNum) {
        $.ajax({
            url: "${path}/getStartExamInfo",
            data: {"pageNum": pageNum},
            type: "GET",
            success: function (pageInfo) {
                var tableData = document.getElementById('tableData');//选取id为test的div元素
                tableData.style.display = 'block';// 隐藏选择的元素
                var pageId = document.getElementById('pageId');//选取id为test的div元素
                pageId.style.display = 'block';// 隐藏选择的元素

                //1、解析并显示教师数据
                buildTable(pageInfo);
                //2、解析并显示分页信息
                build_page_info(pageInfo);
                //3、解析显示分页条数据
                build_page_nav(pageInfo);
            }
        });
    }

    function buildTable(pageInfo) {
        //清空table表格
        $("#teas_table tbody").empty();
        var data = pageInfo.extend.pageInfo.list;
        var result = "";
        for (var i = 0; i < data.length; i++) {
            result += "<tr>"
            result += "<td>" + (i + 1) + "</td>";
            result += "<td>" + data[i].stu_id + "</td>";
            result += "<td>" + data[i].stu_name + "</td>";
            result += "<td>" + data[i].stu_class + "</td>";
            if (data[i].stu_ip != null && data[i].stu_ip != "") {
                result += "<td>" + data[i].stu_ip + "</td><td>";
            } else {
                result += "<td>未绑定ip</td><td>";
            }
            if (data[i].stu_submit == 1) {
                result += "<span class='glyphicon glyphicon-ok'></span>";
            }
            result += "</td></tr>";
        }
        $("#tbody").html(result);
    }

    //解析显示分页信息
    function build_page_info(result) {
        $("#page_info_area").empty();
        $("#page_info_area").append("当前" + result.extend.pageInfo.pageNum + "页,总共" +
            result.extend.pageInfo.pages + "页,总共" +
            result.extend.pageInfo.total + "条记录");
        totalRecord = result.extend.pageInfo.total;
        currentPage = result.extend.pageInfo.pageNum;
    }

    //解析显示分页条，点击分页要能去下一页....
    function build_page_nav(result) {
        //page_nav_area
        $("#page_nav_area").empty();
        var ul = $("<ul></ul>").addClass("pagination");

        //构建元素
        var firstPageLi = $("<li></li>").append($("<a></a>").append("首页").attr("href", "#"));
        var prePageLi = $("<li></li>").append($("<a></a>").append("&laquo;"));
        if (result.extend.pageInfo.hasPreviousPage == false) {
            firstPageLi.addClass("disabled");
            prePageLi.addClass("disabled");
        } else {
            //为元素添加点击翻页的事件
            firstPageLi.click(function () {
                to_page(1);
            });
            prePageLi.click(function () {
                to_page(result.extend.pageInfo.pageNum - 1);
            });
        }

        var nextPageLi = $("<li></li>").append($("<a></a>").append("&raquo;"));
        var lastPageLi = $("<li></li>").append($("<a></a>").append("末页").attr("href", "#"));
        if (result.extend.pageInfo.hasNextPage == false) {
            nextPageLi.addClass("disabled");
            lastPageLi.addClass("disabled");
        } else {
            nextPageLi.click(function () {
                to_page(result.extend.pageInfo.pageNum + 1);
            });
            lastPageLi.click(function () {
                to_page(result.extend.pageInfo.pages);
            });
        }

        //添加首页和前一页 的提示
        ul.append(firstPageLi).append(prePageLi);
        //1,2，3遍历给ul中添加页码提示
        $.each(result.extend.pageInfo.navigatepageNums, function (index, item) {

            var numLi = $("<li></li>").append($("<a></a>").append(item));
            if (result.extend.pageInfo.pageNum == item) {
                numLi.addClass("active");
            }
            numLi.click(function () {
                to_page(item);
            });
            ul.append(numLi);
        });
        //添加下一页和末页 的提示
        ul.append(nextPageLi).append(lastPageLi);

        //把ul加入到nav
        var navEle = $("<nav></nav>").append(ul);
        navEle.appendTo("#page_nav_area");
    }
</script>
</html>